package com.bhprod.comatule.search;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import com.bhprod.comatule.R;
import com.bhprod.comatule.model.Species;

/**
 * Generate the species list content
 * @author MonkeyKiki
 *
 */
public class SpeciesListAdapter extends BaseExpandableListAdapter {
	
	private Context context;
	private List<Species> species = new ArrayList<Species>();
	
	public SpeciesListAdapter(final Context context) {
		this.context = context;
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		 return species.get(groupPosition).getDescription();
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		 return 0;
	}

	/**
	 * Display species detail
	 * @see android.widget.ExpandableListAdapter#getChildView(int, int, boolean, android.view.View, android.view.ViewGroup)
	 */
	@Override
	public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
		String child = (String) getChild(groupPosition, childPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.search_group_child_item, null);
        }
        TextView tv = (TextView) convertView.findViewById(R.id.itemDetailDescription);
        tv.setText(child);
        tv.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);

        return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return 1;
	}

	@Override
	public Object getGroup(int groupPosition) {
		return species.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return species.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	/** Display taxon
	 * @see android.widget.ExpandableListAdapter#getGroupView(int, boolean, android.view.View, android.view.ViewGroup)
	 */
	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
		Species species = (Species) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.search_group_item, null);
        }
        TextView tvName = (TextView) convertView.findViewById(R.id.groupName);
        tvName.setText(species.getName());
        tvName.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
        
        TextView tvChildren = (TextView) convertView.findViewById(R.id.groupChildren);
       	tvChildren.setText("");
        tvChildren.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
        
        // Allow group item AND group checkbox to be clickable
        CheckBox cb = (CheckBox)convertView.findViewById(R.id.groupCheckBox);
        cb.setFocusable(false);
        cb.setTag(groupPosition);
        
        return convertView;
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

	public void setSpecies(List<Species> species) {
		this.species = species;
	}

	public List<Species> getSpecies() {
		return species;
	}

}
